//
//  QDatePickView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/13.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QDatePickView.h"
#import "QDatePickItem.h"
@implementation QDatePickView{

    NSString *_paramStr;//拼出来的时间
    UIControl *_maskView;
    NSString *_timeStr;
    
    NSMutableArray *_itemArr;
    NSInteger _index;
}

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if(self){
        self.frame =CGRectMake((kScreenWidth-280)/2, (kScreenHeight-250)/2, 280, 250);
        
//        _itemArr = [NSMutableArray array];
//        NSDate *date = [NSDate date];
//        NSDateFormatter *df = [[NSDateFormatter alloc] init];
//        [df setDateFormat:@"HH"];
//        int s = [[df stringFromDate:date] intValue];
//        if(s >=0){
//            _dateArr = @[@{@"name":@"今天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        
//        if(s >=2){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=4){
//            _dateArr = @[@{@"name":@"今天",@"dates":@[@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=6){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=8){
//            _dateArr = @[@{@"name":@"今天",@"dates":@[@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=10){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=12){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=14){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=16){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=18){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"20:00-22:00",@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=20){
//             _dateArr = @[@{@"name":@"今天",@"dates":@[@"22:00-24:00"]},@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        if (s >=22){
//             _dateArr = @[@{@"name":@"明天",@"dates":@[@"05:00-06:00",@"06:00-08:00",@"08:00-10:00",@"10:00-11:00",@"14:00-16:00",@"16:00-18:00",@"18:00-20:00",@"20:00-22:00",@"22:00-24:00"]}];
//        }
//        
//        for (NSDictionary * dic in _dateArr) {
//            QDatePickItem *item = [QDatePickItem provinceItemWithDict:dic];
//            [_itemArr addObject:item];
//        }
//        
//        QDatePickItem *item = _itemArr[0];
//        _paramStr = [NSString stringWithFormat:@"%@-%@",item.name,item.dates[0]];
        //[self createUI];
    }
    return self;
}

- (void)setDateArr:(NSArray *)dateArr{
    _itemArr = [NSMutableArray array];
    _dateArr =dateArr;
    for (NSDictionary * dic in _dateArr) {
        QDatePickItem *item = [QDatePickItem provinceItemWithDict:dic];
        [_itemArr addObject:item];
    }
    
    QDatePickItem *items = _itemArr[0];
    NSString *d = items.dates[0];
    if(d.length == 0){
        [_itemArr removeObjectAtIndex:0];
    }
    
    QDatePickItem *item = _itemArr[0];
    _paramStr = [NSString stringWithFormat:@"%@-%@",item.name,item.dates[0]];
    //时间
    _timeStr = item.dates[0];
    [self createUI];

}

- (void)createUI{

    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 4.0;
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:self.bounds];
    [pickerView setDelegate:self];
    [pickerView setDataSource:self];
    
    pickerView.showsSelectionIndicator = YES;
    [self addSubview:pickerView];
    
    for (int i=0; i<2;++i) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i*140, 205, 140, 45)];
        button.tag = i;
        if(i == 0){
            [button setTitle:@"取消" forState:UIControlStateNormal];
            [button setTitleColor:[QColorUtility defaultDeepGrayColor] forState:UIControlStateNormal];
        }else{
            [button setTitle:@"确定" forState:UIControlStateNormal];
            [button setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }
        button.backgroundColor = [UIColor whiteColor];
        button.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
        button.layer.borderWidth = 0.3;
        button.layer.cornerRadius = 3.0;
        [button addTarget:self action:@selector(chooseDateAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];

    }
    _maskView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _maskView.backgroundColor = [UIColor colorWithWhite:0.1f alpha:0.2];
    _maskView.hidden = YES;
    [_maskView addTarget:self action:@selector(maskAction) forControlEvents:UIControlEventTouchUpInside];
    [_maskView addSubview:self];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if(component == 0){
        return _itemArr.count;
    }else{
        QDatePickItem *item = _itemArr[_index];
        return item.dates.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == 0){
        QDatePickItem *item = _itemArr[row];
        return item.name;
    }else{
        QDatePickItem *item = _itemArr[_index];
        NSString *s = [NSString stringWithFormat:@"%@",item.dates[row]];
        return s;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 100;
    }else{
        return 180;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 40;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if(component == 0){
        _index = row;
        [pickerView reloadAllComponents];
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
    
    QDatePickItem *item = _itemArr[_index];
    NSInteger selectRow = [pickerView selectedRowInComponent:1];
    _paramStr = [NSString stringWithFormat:@"%@-%@",item.name, item.dates[selectRow]];
}

- (void)chooseDateAction:(UIButton *)button{
    if(button.tag == 1){
        if(self.dateBlock){
            self.dateBlock(_paramStr,_timeStr);
        }
    }
    [self hidePickView];
}

- (void)showPickView{
    UIViewController *vc = [QViewUtility getCurrentVC];
    [vc.view addSubview:_maskView];
    self.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
    }];
    _maskView.hidden = NO;
}

- (void)hidePickView{
    self.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [_maskView removeFromSuperview];
        [self removeFromSuperview];
        
       [self removeFromSuperview];
        _maskView.hidden = YES;
        
    }];
}

- (void)maskAction{
    [self hidePickView];
}

@end
